# coding:UTF-8
# ________________________
# Project_Name:pyzidonghua
# File_Name:Administrator
# Author:Administrator
# Date_Time:2022/5/16 10:51
# Description:
# ________________________

import unittest  # 导入测试框架
from appium import webdriver #导入
import time
from selenium.webdriver.common.by import By


class CalculatorTest(unittest.TestCase):
    def setUp(self) -> None:
        #手机设置
        desired_caps = {}
        desired_caps['platformName'] = 'Android'  # 系统名称
        desired_caps['platformVersion'] = '7.1.2'  # 系统的版本号
        desired_caps['deviceName'] = 'Android Emulator'  # 设备名称，这里是虚拟机，这个没有严格的规定
        desired_caps['appPackage'] = 'com.netease.cloudmusic'  # APP包名
        desired_caps['appActivity'] = 'com.netease.cloudmusic.activity.LoadingActivity'  # APP入口的activity
        desired_caps['noReset'] = True  # 不重置app的缓存文件
        desired_caps['unicodeKeyboard'] = True  # 设置键盘支持中文输入
        desired_caps['resetKeyboard'] = True  # 重置键盘

        # 连接appium server，告诉appium，代码要操作哪个设备上的哪个APP
        # 启动手机上App
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)



    def test(self):
        time.sleep(10)
        self.driver.find_element(By.ID,'com.netease.cloudmusic:id/searchBar').click() # 点击搜索图标
        time.sleep(5)
        #清空输入框并输入
        self.driver.find_element(By.ID,'com.netease.cloudmusic:id/search_src_text').clear()
        self.driver.find_element(By.ID, 'com.netease.cloudmusic:id/search_src_text').send_keys('孤勇者')
        time.sleep(7)
        #基于坐标点击第一个搜索项（最多可以放5个坐标）
        self.driver.tap([(88,99)])
        time.sleep(5)

        # 向上滑动300个像素
        # swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0)
        self.driver.swipe(270, 480, 270, 180)
        time.sleep(2)

        # 向下滑动300个像素
        self.driver.swipe(270, 480, 270, 780)
        time.sleep(1)

        # 点击结果栏第三项
        self.driver.tap([(200, 473)])
        time.sleep(1)

        # #点击播放
        # self.driver.tap([(271, 918)])

        time.sleep(120)

    def tearDown(self) -> None:
        self.driver.quit()
